package com.singzi.leetcode.N100;

//import second.ListNode;



public class T0061 {
    public static void main(String[] args) {
        ListNode l1 = new ListNode(1);
        ListNode l2 = new ListNode(2,l1);
        ListNode l3 = new ListNode(3,l2);
        ListNode l4 = new ListNode(4,l3);
        ListNode l5 = new ListNode(5,l4);
        Solution0061 solution0061 = new Solution0061();
        ListNode listNode = solution0061.rotateRight(l5,2);
        while (listNode!=null){
            System.out.println(listNode.val);
            listNode = listNode.next;
        }
    }


}
class Solution0061 {
    public ListNode rotateRight(ListNode head, int k) {
        if(head == null) return head;
        if(head.next == null) return head;
        int cont = 1;
        ListNode t = head;
        while(t.next!=null){
            cont++;
            t = t.next;
        }
        k = k%cont;
        ListNode temp = head;
        for(int i = 0;i<k;i++){
            temp = rotateOne(temp);
        }
        return temp;

    }
    public ListNode rotateOne(ListNode head) {
        ListNode t = head;
        while(t.next.next!=null){
            t = t.next;
        }
        ListNode temp = t.next;
        temp.next = head;
        t.next = null;
        head = temp;
        return head;
        //  return temp;

    }
}